package com.kuang.service.Impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.kuang.entity.Student;
import com.kuang.entity.Teacher;
import com.kuang.mapper.DepartmentMapper;
import com.kuang.mapper.MajorMapper;
import com.kuang.service.ExcelService;
import com.kuang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private MajorMapper majorMapper;

    /**
     * excel导入教师表
     * */
    @Override
    public void teacherExcel(MultipartFile file) throws Exception {
        // excel导入教师
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        List<Teacher> teachers = ExcelImportUtil.importExcel(file.getInputStream(), Teacher.class, params);

        // 将密码进行加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        teachers.forEach(teacher->{
            String password = encoder.encode(teacher.getPassword());
            teacher.setPassword(password);
            teacher.setUse_role("ROLE_teacher");
            teacher.setStatus(2);

            Integer departmentById = departmentMapper.findDepartmentById(teacher.getDepartmentId());
            teacher.setDepartmentId(departmentById+"");

            try {
                // 新增教师用户
                userService.saveTeacher(teacher);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        });
    }


    /**
     * excel导入学生表
     * */
    @Override
    public void studentExcel(MultipartFile file) throws Exception {
        // excel导入学生
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        List<Student> students = ExcelImportUtil.importExcel(file.getInputStream(), Student.class, params);
        // 将密码进行加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        students.forEach(student -> {
            String password = encoder.encode(student.getPassword());
            student.setPassword(password);
            student.setUse_role("ROLE_student");
            student.setStatus(2);

            // 获取学院Id
            Integer departmentById = departmentMapper.findDepartmentById(student.getDepartmentId());
            student.setDepartmentId(departmentById+"");
            // 获取专业Id
            Integer majorId = majorMapper.findMajorIdByName(student.getMajorId());
            student.setMajorId(majorId+"");

            try {
                userService.saveStudent(student);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        });
    }
}
